/**
 * 工具脚本, 用于抓取词典数据, 并且存储到mongodb中
 * */
var fs = require('fs');
var mongoConn = require('./store').mongoConn;
var fetcher = require('./wordFetcher');
var config = require('../config.json');

var interval_delay = 500;
var counter = 0;
var uniquer = {};
var tasks = fs.readFileSync(config.dictSrc).toString().split('\n');

var storePolicy = require('./store');
var store = null;

if (config.store.target === 'mongodb') {
  store = storePolicy.mongoStore;
} else if (config.store.target === 'sqlite') {
  store = storePolicy.sqliteStore;
} else {
  throw new Error('Unsupported storage policy');
}

function fetch() {
  if (counter >= tasks.length) {
    clearInterval(interval);
    return;
  }

  var current = tasks[counter].trim();
  if (!uniquer[current]) {
    uniquer[current] = true;
    fetcher(current, function (data) {
      if (data) {
        store(data);
      }
    });
  } else {
    console.log('Not Unique. Skip Word: ' + current);
  }
  counter++;
}

var interval = -1;
mongoConn.find('words', {}, function (err, data) {
  if (err) {
    console.error(err);
  } else {
    for (var i = 0; i < data.length; i++) {
      uniquer[data[i].word] = true;
    }
    interval = setInterval(fetch, interval_delay);
  }
});